package com.lagou.edu.service.impl;

import com.lagou.edu.pojo.Position;
import com.lagou.edu.service.PositionService;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Service
public class PositionServiceImpl implements PositionService {
    @Autowired
    SolrClient solrClient;

    @Override
    public List<Position> queryPositions(String query) throws IOException, SolrServerException {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("df", "positionName");
        solrQuery.setQuery(query);
        QueryResponse queryResponse = solrClient.query(solrQuery);
        SolrDocumentList solrDocuments = queryResponse.getResults();
        long count = solrDocuments.getNumFound();
        System.out.println("查询到的职位数量" + count);
        List<Position> list = new ArrayList<>();
        for(SolrDocument solrDocument : solrDocuments) {
            list.add(solrDocumentToPojo(solrDocument, Position.class));
        }
        SolrDocumentList solrDocuments1 = null;
        if (count < 5) {
            SolrQuery solrQuery1 = new SolrQuery();
            solrQuery1.set("q", "positionAdvantage:美女多||员工福利好");
            QueryResponse queryResponse1 = solrClient.query(solrQuery1);
            solrDocuments1 = queryResponse1.getResults();
            System.out.println("补充推荐职位数量" + solrDocuments1.getNumFound());
            for(SolrDocument solrDocument : solrDocuments1) {
                Position position = solrDocumentToPojo(solrDocument, Position.class);
                if (list.size() < 5 && !list.contains(position)) {
                    list.add(position);
                }
            }
        }

        return list;
    }


    public static <T> T solrDocumentToPojo(SolrDocument document, Class<T> clzz) {
        if (null != document) {
            try {
                Object obj = clzz.newInstance();
                Method m = null;
                Class<?> fieldType = null;

                for (String fieldName : document.getFieldNames()) {
                    //需要说明的是返回的结果集中的FieldNames()比类属性多
                    Field[] filedArrays = clzz.getDeclaredFields();  //获取类中所有属性
                    for (Field f : filedArrays) {
                        if(f.getName().equals(fieldName)){
                            //获取到的属性名
                            f = clzz.getDeclaredField(fieldName);
                            //属性类型
                            fieldType = f.getType();
                            //构造set方法名  setId
                            String dynamicSetMethod = dynamicMethodName(f.getName(), "set");
                            //获取方法
                            m = clzz.getMethod(dynamicSetMethod, fieldType);
                            //获取到的值
                            // 如果是 int, float等基本类型，则需要转型

                            m.invoke(obj, fieldType.cast(document.getFieldValue(fieldName)+""));
                        }
                    }

                }
                return clzz.cast(obj);
            } catch (Exception e) {
                // 请检查schema.xml中的各个field的数据类型与PO类的是否一致
                e.printStackTrace();
            }
        }
        return null;
    }

    //动态构造getXxx setXxx
    public static String dynamicMethodName(String name, String setOrGet){
        String setMethodName = setOrGet
                + name.substring(0, 1).toUpperCase()
                + name.substring(1);
        return setMethodName;
    }
}
